﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Rabbit.Contracts.Interface;
using Rabbit.Domain.Entity;

namespace Rabbit.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class GoodInfoController : ControllerBase
    {
        private readonly IGoodInfoService _goodinfoService;
        private readonly ILogger<GoodInfoController> _logger;

        public GoodInfoController(IGoodInfoService goodinfoService, ILogger<GoodInfoController> logger)
        {
            _goodinfoService = goodinfoService;
            _logger = logger;
        }


        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Upload(IFormFile file)
        {
            string fileName = file.FileName;
            fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + Path.GetExtension(fileName).ToLower();
            string path = Directory.GetCurrentDirectory() + @"\wwwroot\images\" + fileName;
            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            string returnurl = "/images/" + fileName;
            return Ok(JsonConvert.SerializeObject(returnurl));
        }

        /// <summary>
        /// 商品详情添加
        /// </summary>
        /// <param name="good"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult GetGoodInfoAdd([FromBody]Goodinfo good)
        {
            good.CreateTime = DateTime.Now;
            int n=_goodinfoService.GetGoodInfoAdd(good);
            return Ok(n);
        }

        /// <summary>
        /// 商品详情显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetGoodInfoShow()
        {
            var list=_goodinfoService.GetGoodInfoListss();
            return Ok(list);
        }

        /// <summary>
        /// 跳转页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetGoodInfoImage(int id)
        {
            var list=_goodinfoService.GetGoodInfoImage(id);
            return Ok(list);
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetFindGoodinfo(int id)
        {
            var list=_goodinfoService.GetFindGoodinfo(id);
            return Ok(list);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="good"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult GetGoodInfoUpdate(Goodinfo good)
        {
            var list=_goodinfoService.GetGoodInfoUpdate(good);
            return Ok(list);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult GetDelGoodInfo(int id)
        {
            var list = _goodinfoService.GetDelGoodInfo(id);
            return Ok(list);
        }
    }
}
